Imports System
Imports System.IO
Imports System.Diagnostics
Imports System.Collections
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.Xml


Namespace AddinCustomAction

    '/ <summary>
    '/ Custom action for add-in deployment.
    '/ </summary>
    <RunInstaller(True)> _
    Partial Public Class AddinInstaller
        Inherits System.Configuration.Install.Installer

        '/ <summary>
        '/ Namespace used in the .addin configuration file.
        '/ </summary>         
        Private Const ExtNameSpace As String = "http://schemas.microsoft.com/AutomationExtensibility"

        '/ <summary>
        '/ Constructor. Initializes components.
        '/ </summary>
        Public Sub New()
        End Sub

        '/ <summary>
        '/ Overrides Installer.Install, which will be executed during install process.
        '/ </summary>
        '/ <param name="savedState">The saved state.</param>
        Public Overrides Sub Install(ByVal savedState As IDictionary)
            ' Uncomment the following line, recompile the setup
            ' project and run the setup executable if you want
            ' to debug into this custom action.
            MyBase.Install(savedState)

            ' Parameters required to pass in from installer
            Dim productName As String = Me.Context.Parameters("ProductName")
            Dim assemblyName As String = Me.Context.Parameters("AssemblyName")

            ' Setup .addin path and assembly path
            Dim addinTargetPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Visual Studio 2005\Addins")
            Dim assemblyPath As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
            Dim addinControlFileName As String = assemblyName + ".Addin"
            Dim addinAssemblyFileName As String = assemblyName + ".dll"

            Try
                Dim dirInfo As DirectoryInfo = New DirectoryInfo(addinTargetPath)
                If Not dirInfo.Exists Then
                    dirInfo.Create()
                End If

                Dim sourceFile As String = Path.Combine(assemblyPath, addinControlFileName)
                Dim doc As XmlDocument = New XmlDocument()
                doc.Load(sourceFile)
                Dim xnm As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
                xnm.AddNamespace("def", ExtNameSpace)

                ' Update Addin/Assembly node
                Dim node As XmlNode = doc.SelectSingleNode("/def:Extensibility/def:Addin/def:Assembly", xnm)
                If Not node Is Nothing Then
                    node.InnerText = Path.Combine(assemblyPath, addinAssemblyFileName)
                End If

                ' Update ToolsOptionsPage/Assembly node
                node = doc.SelectSingleNode("/def:Extensibility/def:ToolsOptionsPage/def:Category/def:SubCategory/def:Assembly", xnm)
                If Not node Is Nothing Then
                    node.InnerText = Path.Combine(assemblyPath, addinAssemblyFileName)
                End If

                doc.Save(sourceFile)

                Dim targetFile As String = Path.Combine(addinTargetPath, addinControlFileName)
                File.Copy(sourceFile, targetFile, True)

                ' Save AddinPath to be used in Uninstall or Rollback
                savedState.Add("AddinPath", targetFile)
            Catch ex As Exception
                MsgBox("Custom action for installer failed!" & Environment.NewLine & Environment.NewLine & "Error:" & Environment.NewLine & ex.Message, MsgBoxStyle.Exclamation, "Custom action for installer failed")
                MsgBox(ex.StackTrace)
                MsgBox(ex.Source)
            End Try
        End Sub

        '/ <summary>
        '/ Overrides Installer.Rollback, which will be executed during rollback process.
        '/ </summary>
        '/ <param name="savedState">The saved state.</param>
        Public Overrides Sub Rollback(ByVal savedState As IDictionary)
            '//Debugger.Break();
            MyBase.Rollback(savedState)

            Try
                Dim fileName As String = CType(savedState("AddinPath"), String)
                If File.Exists(fileName) Then
                    File.Delete(fileName)
                End If
            Catch ex As Exception
                Debug.WriteLine(ex.ToString())
            End Try
        End Sub

        '/ <summary>
        '/ Overrides Installer.Uninstall, which will be executed during uninstall process.
        '/ </summary>
        '/ <param name="savedState">The saved state.</param>
        Public Overrides Sub Uninstall(ByVal savedState As IDictionary)
            '//Debugger.Break();
            MyBase.Uninstall(savedState)

            Try
                Dim fileName As String = CType(savedState("AddinPath"), String)
                If File.Exists(fileName) Then
                    File.Delete(fileName)
                End If
            Catch ex As Exception
                Debug.WriteLine(ex.ToString())
            End Try
        End Sub
    End Class
End Namespace

'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel (http://www.KamalPatel.net) 
'----------------------------------------------------------------
